﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Order
{
    public class Express_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Order/express_edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public IExpressService _express { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            ChkAdminLevel("order_express", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            this.action = Request.Query["action"];
            if (!string.IsNullOrEmpty(this.action) && this.action == CMSEnums.ActionEnum.Edit.ToString())
            {
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    ViewBag.ClientScript = JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_express.Exists(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }
        // GET: /admin/Express_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            else
            {
                Express model = new Express();
                ViewData["model"] = model;
            }
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("order_express", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                ViewBag.ClientScript = JscriptMsg("修改物流配送成功！", "../express_list/index");
            }
            else //添加
            {
                ChkAdminLevel("order_express", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                ViewBag.ClientScript = JscriptMsg("添加物流配送成功！", "../express_list/index");
            }
            return result;
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Express model = _express.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Express model = new Express();
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.express_code = Request.Form["txtExpressCode"].FirstOrDefault().Trim();
            model.express_fee = Utils.StrToDecimal(Request.Form["txtExpressFee"].FirstOrDefault().Trim(), 0);
            model.website = Request.Form["txtWebSite"].FirstOrDefault().Trim();
            model.remark = Utils.ToHtml(Request.Form["txtRemark"]);
            model.is_lock = Request.Form["cbIsLock"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 0 : 1;
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 99);

            if (_express.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加配送方式:" + model.title); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            Express model = _express.GetModel(_id);
            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.express_code = Request.Form["txtExpressCode"].FirstOrDefault().Trim();
            model.express_fee = Utils.StrToDecimal(Request.Form["txtExpressFee"].FirstOrDefault().Trim(), 0);
            model.website = Request.Form["txtWebSite"].FirstOrDefault().Trim();
            model.remark = Utils.ToHtml(Request.Form["txtRemark"]);
            model.is_lock = Request.Form["cbIsLock"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 0 : 1;
            model.sort_id = Utils.StrToInt(Request.Form["txtSortId"].FirstOrDefault().Trim(), 99);

            if (_express.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改配送方式:" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion
    }
}
